
class PageBase{
  static REAL_CLZ = 'real-panel';
  static HIST_CLZ = 'his-panel';
  static FRM_DEFAULT = -1;
  static STR_DEF = ''
  constructor(id, taskNmPrefix, frmTypeNmPrefix) {
    this.id = id;
    this.taskNmPrefix = taskNmPrefix;
    this.frmTypeNmPrefix = frmTypeNmPrefix;
    this.body = document.querySelector('body');
    this.realUrl = ()=>`omsp.realtime?ins=${this.id}/${$STATION}`

    this.initReal();
    this.initHistory();
    this.initFrm()
    this.initData();
    
  }

  initData() {
    this.rstTaskD()
    this.rstFrmsD()
  }
  
  
  rstTaskD() {
    this.task = PageBase.STR_DEF;
  }
  rstFrmsD() {
    this.frm = PageBase.FRM_DEFAULT;
    this.fmax = PageBase.FRM_DEFAULT;
    this.fmin = PageBase.FRM_DEFAULT;
  }

  setFrms() {
    this.frmMaxE.val(this.fmax === -1? '': this.fmax);
    this.frmNumE.val(this.frm===-1?'': this.frm);
    this.frmRangeE.val(this.frm);
    this.frmNumE.attr({max: this.fmax, min: this.fmin});
    this.frmRangeE.attr({max: this.fmax, min: this.fmin});
  }
  setFrm(d) {
    this.frm = d.frm
    this.setInfo(d)
  }
  
  setInfo(d) {
  
  }
  taskTmpl(){
    return this.checkBlankStr(this.task)? '': `${this.taskNmPrefix}${parseTask(this.task)}`;
  }
  frmTypeTmpl(){
    return this.checkBlankStr(this.frmType) ? '' : `${this.frmTypeNmPrefix}${this.frmType}`;
  }

  checkBlankStr(v) {
    return v==undefined || v === PageBase.STR_DEF;
  }
  resetHist() {
    this.initData();
    this.clearTasks();
  }
  resetReal(){
    this.initData()
    this.setTask()
  }
  
  setTask() {
    this.taskE.html(this.taskTmpl())
  }
  
  setFrmTypes(d) {
    let t = d || this.frmTypes
    this.frmTypesE.html(t.map(v=>v).join(''))
  }
  clearRealAll() {
    this.clearTask()
  }
  
  clearHistAll() {
    this.clearTasks()
    this.clearFrms()
  }
  

  clearTasks() {
    this.tasksE.html('');
    this.clearTask();
  }

  clearTask() {
    this.clearFrms()
    this.clearFrm();
  }

  clearFrmTypes() {
    this.frmTypesE.html(PageBase.STR_DEF);
  }

  clearFrms() {
    this.rstFrmsD()
    this.setFrms()
    
    this.clearFrm();
  }

  clearFrm() {
    this.infoE.html('');
  }
  resetReal() {
    this.taskE.html(this.taskTmpl());
  }
  initReal(){
    this.taskE = $('#r-task');
    this.frmTypeE = $('#r-frm-type');
  }
  real(){
    this.realUI();
    this.initData()
    this.resetReal();
    this.connect()
  }

  history() {
    this.historyUI();
    this.close()
    this.initData();
  }
  initHistory(){
    this.dateE = $('#date');
    this.tasksE = $('#h-tasks');
    this.frmTypesE = $('#h-frm-types');
    this.frmMaxE = $('#frm-max');
    this.frmNumE = $('#frm-num');
    this.frmRangeE = $('#frm-range')
  }

  initFrm() {
    this.frmcE = document.getElementById('c');
    this.infoE = $('#info')
  }
  realUI(){
    $REAL_FLAG = true;
    this.body.classList = PageBase.REAL_CLZ;
  }
  
  normalFrmId(v) {
    try {
      let frmIdx = parseInt(v)
      if(frmIdx > this.fmax) frmIdx = this.fmax
      if(frmIdx < this.fmin) frmIdx = this.fmin
      return frmIdx
    } catch (e) {
      return 0
    }
  }
  historyUI(){
    $REAL_FLAG = false;
    this.body.classList = PageBase.HIST_CLZ;
  }
   download() {
      if(this.task==='' ) return;
      let parms = parseTask(this.task)
      let filename = `${parms.st}-${parms.orb}-${$INS}.h5`
      let url = `/-/omsp.downf?f=${this.task}`
      downFile(url, filename)
    }
  
  connect() {
    this.close()
    this.ws = new WsEx(this.realUrl())
    this.ws.onmessage = this.msg.bind(this)
    this.ws.onerror = this.error.bind(this)
  }
  close(){
    if(this.ws) this.ws.close()
    delete this.ws
  }
  
  error() {
    this.close()
    if(this.timeoutId) clearTimeout(this.timeoutId)
    this.timeoutId = setTimeout(this.connect.call(this), 3000)
  }
  
  setTask(d) {
    let p = parseTask(d.task)
    this.task = d.task+'/.h5'
    this.taskE.html('任务：'+p.label)
    
  }
  
  msg(e) {
    let d = JSON.parse(e.data)
    if (d.task) {
      setTask(d);
      return;
    }
    
    this.setFrm(d)
  }
    
}


